
from  config.config import host2,appid,secret
import requests,copy,json,time,hashlib,yaml,time,random
#有参数的请求头headers
def getHeaders(payload1,timestemp,host):
    payload=copy.copy(payload1)
    payload["appId"]=appid
    payload["timestamp"]=timestemp
    url=f"{host}/pairKey/generateThirdSign"
    res = requests.post(url,json=payload)
    sign=res.json()['data']
    headers={"appid":appid,"timestamp":timestemp,"sign":sign}
    return headers
#没有参数的请求头
def getHeadersNoPara(timestemp,host):
    payload={}
    payload["appId"]=appid
    payload["timestamp"]=timestemp
    url=f"{host}/pairKey/generateThirdSign"
    res = requests.post(url,json=payload)
    sign=res.json()['data']
    headers={"appid":appid,"timestamp":timestemp,"sign":sign}
    return headers

#获取时间戳
def getTimesTemp():
    timesTmp = time.time();
    timesTmp = str(int(round(timesTmp * 1000)))
    return timesTmp

#json数据格式化展示
def jsonformat(data):
    data = json.dumps(data, sort_keys=True, indent=2)
    data = data.encode('utf-8').decode('unicode_escape')
    return data

#获取 格式化成日期 2016-03-20 11:45:39形式
def getNow():
    now=time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    return now

#冒泡排序,将参数进行升序排列
def bubbleSort(arr):
    n = len(arr)
    # 遍历所有数组元素
    for i in range(n):
        for j in range(0, n - i - 1):
            if arr[j] > arr[j + 1]:
                arr[j], arr[j + 1] = arr[j + 1], arr[j]

def md5(key):
    input_name = hashlib.md5()
    input_name.update(key.encode("utf-8"))
    str=input_name.hexdigest()
    upStr=str.upper()
    return upStr

def get_yaml_data(fileDir,name):
    resList=[]
    #1读取ymal文件
    fo = open(fileDir,'r',encoding='utf-8')
    #2使用yaml读取
    res=yaml.load(fo,Loader=yaml.FullLoader) #[(请求body,响应body)]
    method=res['method']
    keys=method.keys()
    names=method[name]
    if name in keys:
        for i in range(0, len(method[name])):
            resList.append((method[name][i]['data'], method[name][i]['resp'],method[name][i]['detail']))
    return resList

#有参数的请求头
def getHeadersNow(payload1,timestemp):
    payload=copy.copy(payload1)
    payload["appId"]=appid
    payload["timestamp"]=timestemp
    #根据规则生成sign
    #第一步参数排序
    arr=list(payload.keys())
    bubbleSort(arr)
    #第二步参数appKeyXXXbusiTypeXXXcontentXXXtimestampXXX, str=secret+str+secret
    str=""
    for i in range(len(arr)):
        str+=arr[i]+payload[arr[i]]
    #第三步将secret拼接到第⼆步字符串的⾸尾两端
    str = secret + str + secret
    #第四步骤将拼接的资字符串进行md5加密32位后转为换大写
    sign=md5(str)
    headers={"appid":appid,"timestamp":timestemp,"sign":sign}
    return headers
#没有参数的请求头
def getHeadersNoParaNow(timestemp):
    payload={}
    payload["appId"]=appid
    payload["timestamp"]=timestemp
    # 根据规则生成sign
    # 第一步参数排序
    arr = list(payload.keys())
    bubbleSort(arr)
    # 第二步参数appKeyXXXbusiTypeXXXcontentXXXtimestampXXX
    str = ""
    for i in range(0,len(arr)):
        str += arr[i] + payload[arr[i]]
        # 第三步将secret拼接到第⼆步字符串的⾸尾两端
    str = secret + str + secret
    # 第四步骤将拼接的资字符串进行md5加密32位后转为换大写
    sign = md5(str)
    headers={"appid":appid,"timestamp":timestemp,"sign":sign}
    return headers

from bs4 import BeautifulSoup
import urllib.request
import sys

def getHtmla(html_doc,ip):
    if len(sys.argv) > 1:
        website = sys.argv[1]
        if (website is not None):
            html_doc = sys.argv[1]
    # 获取请求
    req = urllib.request.Request(html_doc)
    # 打开页面
    webpage = urllib.request.urlopen(req)
    # 读取页面内容
    html = webpage.read()
    # 解析成文档对象
    soup = BeautifulSoup(html, 'html.parser')  # 文档对象
    # 非法URL 1
    invalidLink1 = '#'
    # 非法URL 2
    invalidLink2 = 'javascript:void(0)'
    # 集合
    result = {}
    # 计数器
    mycount = 0
    # 查找文档中所有a标签
    for k in soup.find_all('a'):
        # print(k)
        # 查找href标签
        link = k.get('href')
        linkContent=k.string
        # 过滤没找到的
        if (link is not None):
            # 过滤非法链接
            if link == invalidLink1:
                pass
            elif link == invalidLink2:
                pass
            elif link.find("javascript:") != -1:
                pass
            else:
                mycount = mycount + 1
                result[linkContent]=link

    for k,v in result.items():
        if v.startswith('/'):
            v=f'{ip}{v}'
        try:
            r = requests.get(v)
            # 判断是否无法打开
            #print(f'当前正在检测的网站地址为：{k}\n{v}')
            rnumber=random.randint(1,2)
            time.sleep(rnumber)
            if (r.status_code != 200 and r.status_code!=403):
                print(f'死链接地址为： code为{r.status_code}  {k}\n{v}')
        except Exception as ex:
            print(f'出现异常：异常的网站为:{k}\n{v}\n异常信息为{ex}')
    print(f'查看{html_doc}网站完成!')



if __name__ == '__main__':
    '''url地址查询数据存在的死链接'''
    # url='http://10.20.1.66:8082/html/view/default/site_nav.html?navPage=11'
    # ipAddress='http://10.20.1.66:8082'
    # getHtmla(url,ipAddress)
    '''获取yaml文件的数据'''
    # res=get_yaml_data('../../data/block.yaml','add')
    # print(res)
    times=getTimesTemp()
    print(times)
    sign=getHeadersNoParaNow(times)
    print(sign)

